<?php

namespace Common\Model;
use Think\Model;

class OrderGoodsModel extends Model{

    /**
     * 新增或更新
     *
     * @return boolean fasle 失败 ， int 成功 返回完整的数据
     */
    public function update($data)
    {
        $data = $this->create($data);
        if (empty($data)) {
            return false;
        }
        if (empty($data['id'])) {
            $id = $this->add();
            if (! $id) {
                $this->error = '新增出错';
                return false;
            }
        } else {
            $result = $this->where(array( 'id' => $data['id'] ))->save($data);
            if (false === $result) {
                $this->error = '更新出错';
                return false;
            }
            $id = $data['id'];
        }

        return $id;
    }

    public function getMemberOrderGoods($memberID, $menuID)
    {
        if (empty($memberID) || empty($menuID)){
            return false;
        }

        $join = 'INNER JOIN __ORDER__ AS o ON g.order_id = o.order_id';
        $field = 'g.order_id,g.goods_name,g.goods_num';

        $where= array();
        $where['g.menu_id'] = $menuID;
        $where['g.member_id'] = $memberID;
        $where['o.pay_state'] = 1;
        $result = $this->table('__ORDER_GOODS__ AS g')->join($join)->field($field)->where($where)->select();
        return $result;

    }

    public function getCommentGoods($memberID)
    {
        if (empty($memberID)){
            return false;
        }

        $where= array();
        $where['order_state'] = 4;
        $where['member_id'] = $memberID;
        $where['comment_star'] = 0;
        $result = $this->field('id')->where($where)->find();
        return $result;

    }

}
